home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Misc / SC / Source / scFrontend.m < prev    next >
Encoding:
Text File  |  1994-05-15  |  3.1 KB  |  142 lines

  1. #import "scFrontend.h"
  2. #import <appkit/Application.h>
  3. #import <appkit/Pasteboard.h>
  4. #import <appkit/Speaker.h>
  5. #import <appkit/Listener.h>
  6. #import <defaults/defaults.h>
  7. #import <libc.h>
  8. #import <string.h>
  9. #import <mach.h>
  10.  
  11.  
  12. @implementation scFrontend : Object
  13.  
  14. - copyString:(char *)s {
  15.   id p = [Pasteboard new];
  16.   [p declareTypes:&NXAsciiPboard num:1 owner:self];
  17.   [p writeType:NXAsciiPboard data:s length:strlen(s)];
  18.   return self;
  19. }
  20.  
  21. - launchTerminal:(char *)program {
  22.   id p = [NXApp appSpeaker];
  23.   port_t t = NXPortFromName("Terminal",NULL);
  24.   int ok;
  25.   if (t==PORT_NULL) return self;
  26.   [p setSendPort:t];
  27.   [self copyString:program];
  28.   (void)[p msgPaste:&ok];
  29.   return self;
  30. }
  31.  
  32.  
  33. static id openFile(const char *directory, const char *name)
  34. /*
  35.  * Opens a file with the given name in the specified directory.
  36.  * Returns self if successful, nil otherwise.
  37.  */
  38. {
  39.     char buffer[MAXPATHLEN+1], path[MAXPATHLEN+1], cmd[MAXPATHLEN+1];
  40.  
  41.     if (name) {
  42.         if (!directory) {
  43.             directory = ".";
  44.         } else if (*directory != '/') {
  45.             strcpy(buffer, "./");
  46.             strcat(buffer, directory);
  47.             directory = buffer;
  48.         }
  49.         if (!chdir(directory) && getwd(path)) {
  50.         if (strlen(name)) {
  51.             sprintf(cmd,"cd %s;sc %s\n", path, name);
  52.         } else {
  53.             sprintf(cmd,"cd %s;sc\n", path);
  54.         }
  55.             return [[NXApp delegate] launchTerminal:cmd];
  56.     }
  57.     }
  58.  
  59.     return nil;
  60. }
  61.  
  62. /* Public methods */
  63.  
  64. - openBlankSheet:sender
  65. {
  66.     openFile(".","");
  67.     [NXApp hide:sender];
  68.     return self;
  69. }
  70.     
  71. /*
  72.  * Application object delegate methods.
  73.  */
  74.  
  75. - appDidInit:sender
  76. /*
  77.  * Check for files to open specified on the command line.
  78.  * If there are no open documents, then open a blank one.
  79.  */
  80. {
  81.     int i;
  82.     char buffer[MAXPATHLEN+1];
  83.     char *directory, *name, *ext;
  84.     
  85.     if (NXArgc > 1) {
  86.         for (i = 1; i < NXArgc; i++) {
  87.             strcpy(buffer, NXArgv[i]);
  88.             ext = strrchr(buffer, '.');
  89.             if (!ext || strcmp(ext, ".pdsc")) strcat(buffer, ".pdsc");
  90.             name = strrchr(buffer, '/');
  91.             if (name) {
  92.                 *name++ = '\0';
  93.                 directory = buffer;
  94.             } else {
  95.                 name = buffer;
  96.                 directory = NULL;
  97.             }
  98.             openFile(directory, name);
  99.         [NXApp hide:self];
  100.         }
  101.     } else {
  102.     if (gotFile!=YES)
  103.         [[NXApp delegate] openBlankSheet:self];
  104.     }
  105.  
  106.     return self;
  107. }
  108.  
  109. - (int)appOpenFile:(const char *)path type:(const char *)type
  110. /*
  111.  * This method is performed whenever a user double-clicks on an icon
  112.  * in the Workspace Manager representing a PD SC program document.
  113.  */
  114. {
  115.     char *name;
  116.     char directory[MAXPATHLEN+1];
  117.  
  118.     if (type && !strcmp(type, "pdsc")) {
  119.         strcpy(directory, path);
  120.         name = strrchr(directory, '/');
  121.         if (name) {
  122.             *name++ = '\0';
  123.             if (openFile(directory, name)) {
  124.         [NXApp hide:self];
  125.                 return gotFile= YES;
  126.             }
  127.         }
  128.     }
  129.  
  130.     return NO;
  131. }
  132.  
  133. - (BOOL)appAcceptsAnotherFile:sender
  134. /*
  135.  * We accept any number of appOpenFile:type: messages.
  136.  */
  137. {
  138.     return YES;
  139. }
  140.  
  141. @end
  142.